In the last video we discovered that investing in the S&P 500 fund investment period of three years
can be a really volatile and risky investment depending on when and at which market conditions you are
investing.
So in this video we want to calculate the rolling return of the S&P 500 for different investment periods
from one year to 20 years.
So in other words we will calculate the annualized average monthly returns with rolling windows from
a twelve month to 240 month.
So that's 20 years and we want to discover whether there is kind of a time diversification effect.
People typically say that stocks are long term investment Bear and the good days you recover the losses
made and the bad days and we try to prove and visualize this in the next minutes.
So first of all we import us and we also import met plot lip.
Then we are again working with the S&P 500 see a three fire.
So let's have a look here.
So we are working only here with the closing prices and we can also call you the info method.
So this is actually nothing new.
And we can also plot um the S&P 500 price from the 70s to today.
So this was a pretty good investment if you invested in the S&P 500 in the 70s and again also in this
video we are working with monthly returns and therefore we need to recycle but here the closing prices
to monthly and we calculate the percentage change.
So this is nothing new.
And let's have a look at the last five monthly returns here in 2000 and 18 and to avoid any confusion
we rename here the column Labor Forum.
Close the two monthly returns based on closing prices.
So this is actually also nothing new.
So let's again have a look here.
So now our monthly returns column has that label monthly returns.
And actually in the last video we calculated the rolling average return and the rolling standard deviation
of returns for the last to 36 months.
So let's repeat this here for the rolling returns.
So we you see the rolling method and we multiply our desired investment period.
So here three years by twelve twelve months.
So in total we have 36 months.
And then we calculate the rolling mean and by multiplying this uh with 12 we get actually annualized.
Rolling mean returns.
So this is also nothing new.
Let's do this here.
And apparently in the first thirty five months we have any end value.
So no surprise here and now we actually want to do the same for investment periods of one year five
years 10 years and 20 years.
And we use here a for loop so we iterate over the investment period.
So four years in uh the lists here.
So we have 1 3 5 10 and 20 year and for each of these five investment periods you want to add here to
our monthly returns data frame a new column.
So we want to add to your column for the one year rolling return for our three year rolling return 5
10 and 20 year.
And we can actually create the columns with this column labeled here with this code here and let's have
a look at an example.
So let's take year the one year period.
So we are creating here the one year column with the column label one year and what we are doing actually
we take here the column monthly returns.
So these are the monthly returns.
Then we apply the rolling method and for the rolling window we are actually calculating here a yes times
twelve.
So in the very first iteration yes that's equal to 1.
So one time twelve gives us a twelve as a window that we are calculating the mean and we are analyzing
it and thus we are doing here for all five periods.
So one year three year five year 10 year and 20 year.
And by doing so we're creating five columns.
So let's do this year and let's have a look here at the last five rows of our data frame.
So here we have added successfully the columns one year three year five year 10 year and 20 year and
let's have a look at the very last row.
So this is December to 18.
And if we have a look here at the column one year.
So we have a minus 6 percent and this is actually the annualized the mean return over the last 12 months
and consequently here in the column 3 year we have a seven point one percent.
And this is actually the annualized the mean return for the preceding thirty six month period form to
a sixteen to to 18.
And that's also if I look at the 20 year column.
So here we have an annualized monthly return of 4.5 percent and this is actually the mean return over
the last 240 months.
And we can also create a plot.
Um this this uh five columns.
So we are selecting the last five columns.
And uh let's see what we got here so uh here we can see on the x axis the respective date and on the
y axis.
The annualized return.
And here we have actually five lines uh which are actually are rolling statistics are rolling returns.
So the blue line here is the the rolling return on the investment period of one year.
And this is actually quite volatile as you can see here.
So if you have invested in 2 8 and divested and 2 9 then in this case you've got a negative annualized
return of uh let's say minus 50 percent.
And in contrast the few invested and 2 9 2 to 10.
Then you analyze them mean return would be around 40 percent and you can also see here that the longer
the investment period the less volatile our graph is here.
So we have here the green graph within the investment period of three years.
And the yellow curve which is not volatile at all actually was on the rolling investment period of 20
years.
And we can also create subplots to have a better view here and we can do a service of the subplots perimeter
and pass see a true
so for each investment period we have here now a subplot.
And we can also increase here the figure size to let's say 50 and uh 40
so we are starting here with a one year investment period and the rolling return and uh the rolling
return this year between minus 50 percent and plus 40 percent.
Then we have a look here at the three year rolling return.
And this is already here between a minus let's say 15 percent and plus the 25 percent.
Then we have the five year rolling return and the 10 year rolling return.
And then finally the 20 year rolling return.
And as we can see here on the left hand side the scale is different than for the other graphs subplots.
So the 20 year annualized rolling return is between a plus 5 percent and 14 percent.
So in the history of the S&P 500 index there's actually no 20 year period where we actually made the
losses.
So after 20 years we are always positive actually so we gained or we increased our investment.
And uh this is uh different to uh let's say the 10 year or the five year or even the the one year investment
period.
So here it's quite likely that over one year we make losses also.
And we can even better see this effect by passing true to the share by parameter
and by doing so for each subplot we I have actually the uh y axis of uh the one year period.
So here we have the returns between minus 40 and the 40 percent and we can see here.
So one year is quite volatile and the SBA increases the investment period the volatility decreases.
So this is quite obvious here.
So finally with 20 years period there's hardly any volatility here so as a summary we can agree that
that's kind of a time diversification effect.
And uh the longer the investment period the less likely it is that you will suffer losses.
So actually stocks a risky investment and it's quite likely that over a period of 1 to 5 for 10 years
you made some losses but were periods of 15 or 20 years.
It's uh at least if you have a look at historical data it's quite unlikely that you'll lose money over
that long time periods and uh with this we are finished with this video and I hope to see you also and
the next one by.